import { h } from "koishi"
export const name = "welcome"
export async function apply(ctx) {
  new Greeter(
    ctx.platform("onebot").guild("698545476"),
    '欢迎加入本群。发送“/x [文本]”以翻译〇改希顶语。发送“/help”查看更多功能。'
  )
  new Greeter(
    ctx.platform("onebot").guild("754732657"),
    '欢迎加入希顶模组服务器交流群，请到群文件下载模组，服务器地址见群公告。发送“/x [文本]”可翻译〇改希顶语。发送“/help”查看更多功能。'
  )
}

class Greeter {
  bot = null
  channelId = null
  messageIds = null
  time = -Infinity

  constructor(ctx, text) {
    this.ctx = ctx
    this.text = text
    this.dispose = ctx.on("guild-member-added", () => ctx.sleep(3000).then(() => this.greet()))
  }

  async greet(session) {
    const messageIds = await session.send(this.text)
    const now = Date.now()
    if (now - this.time < 300_000) {
      this.messageIds.forEach(m => {
        this.bot.deleteMessage?.(this.channelId, m).catch(e => this.ctx.logger.warn(e))
      })
    }
    this.bot = session.bot
    this.channelId = session.channelId
    this.messageIds = messageIds
    this.time = now
  }
}
